// pages/QnA.js
// 获取应用实例
const app = getApp()
import util from '../../utils/util.js'


Page({
  /**
   * 页面的初始数据
   */
  data: {
    //已回答
    answered: false,
    //当前题号
    nowIndex: 0,
    //题目
    topic:{},
    //答案
    answer:{},
    //选项结果样式
    optionClassList: [".result", ".result", ".result", ".result"],

    //下标转字母数组
    index2letter: ["A. ", "B. ", "C. ", "D. "],
    //触摸坐标
    touchS: [0,0],
    touchE: [0,0],

    //分数比例百分比
    scorePercentage: 57,
    //答题进度百分比
    progressValue: 0,
    //滚动栏高度, 100 - 863 * 100 / app.globalData.rpxHeight, 863 为其他元素总高度
    scrollHeight: 100 - 86300 / app.globalData.rpxHeight,

    //PV
    getting: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /* 获取题目 */
    this.setData({
      //获取用户信息
      userInfo: app.globalData.userInfo,
      //请求题目所需数据
      topicRequireBody:{
        "gradeName": "三年级",
        "groupName": "小学",
        "roomId": util.uuid(),
        "subject": "数学",
        "tenantId": "746112002023227392"
      }
    })
    this.getRandomTopic(this.reflushTopic)
  },

  /* UI 交互 */
  /**
   * 用户按下
   */
  touchStart: function(e){
    let sx = e.touches[0].pageX
    let sy = e.touches[0].pageY
    this.data.touchS = [sx,sy]
    this.data.touchE = [sx,sy]
  },
  /**
   * 用户滑动
   */
  touchMove: function(e){
    let sx = e.touches[0].pageX
    let sy = e.touches[0].pageY
    this.data.touchE = [sx, sy]
  },
  /**
   * 用户松开
   */
  touchEnd: function(e){
    let start = this.data.touchS
    let end = this.data.touchE
    if((start[0] > end[0] + 50) && this.data.answered) this.slide2NextTopic()
  },
  
  /**
   * 获取下一题
   */
  slide2NextTopic: function(){
    if(!this.data.answered || this.data.getting) return
    this.data.getting = true
    if(this.data.answer.nowIndex < 5){
      /* 获取题目 */
      this.getRandomTopic(this.reflushTopic)
    }else{
      /* 跳转页面 */
      app.globalData.playerSum = this.data.answer.playerSum
      app.globalData.robotSum = this.data.answer.robotSum
      wx.redirectTo({
        url: '../GameOver/GameOver'
      })
    }
  },

  /**
   * 点击选项
   */
  touchOption: function(option){
    if(this.data.answered || typeof(option.target.dataset.option) == "undefined") return
    this.setData({answered: true})
    this.answerTopic(option, this.reflushAnswers)
  },

  /* UI 更新 */
  /**
   * 更新题目
   */
  reflushTopic: function(res){
    this.setData({
      /* ++题目数 */
      nowIndex: this.data.nowIndex + 1,
      /* ++答题进度 */
      progressValue: (this.data.nowIndex + 1) * 20,
      /* 获取题目内容 */
      topic: res.data.data,
      /* 清空答题情况 */
      optionClassList: [".result", ".result", ".result", ".result"],
      /* 恢复为未回答状态 */
      answered: false,
    })
    this.data.getting = false
  },
  /**
   * 更新答案
   */
  reflushAnswers: function(selected, right){
    /* 更新选项情况 */
    this.data.optionClassList[selected] = ".result.err"
    this.data.optionClassList[right] = ".result.cor"
    /* 计算分数比例 */
    let scorePercentage = (57 + (this.data.answer.playerSum - this.data.answer.robotSum) * 8.6)
    this.setData({
      /* 刷新分数比例 */
      scorePercentage: scorePercentage,
      /* 刷新选项标记 */
      optionClassList:this.data.optionClassList,
    })
    /* 弹出土司 */
    this.answerToast(this.data.answer.playerResult)
  },
  /**
   * 回答情况提示
   */
  answerToast: function(result){
    /* 回答正确 */
    if(result){
      wx.showToast({
        title: '回答正确',
        icon: 'success',
        duration: 1000
      })
    /* 回答错误 */
    }else{
      wx.showToast({
        title: '回答错误',
        icon: 'none',
        duration: 1000
      })
    }
  },

  /* 后端交互 */
  /**
   * 获取题目
   */
  getRandomTopic: function(reflushTopic){
    let thisPage = this
    /* 发送请求 */
    wx.request({
      url: 'http://112.74.105.17:20003/question-bank/wx/answer/stem',
      method:"POST",
      dataType:"json",
      data: this.data.topicRequireBody,
      success(res){
        if(res.data.code===200){
          /* 更新 UI */
          reflushTopic(res)
        }
        else{
          wx.showToast({
            title: '正在获取数据',
            icon: 'loading',
            duration: 1000
          })
          thisPage.getRandomTopic(reflushTopic)
        }
      },
      fail(){
        wx.showToast({
          title: '正在获取数据',
          icon: 'loading',
          duration: 1000
        })
        thisPage.getRandomTopic(reflushTopic)
      } 
    })
  },
  /**
   * 回答问题
   */
  answerTopic: function(option, reflushAnswers){
    let thisPage = this
    let cont = option.target.dataset.option
    let roomId = this.data.topicRequireBody.roomId
    let rightIndex = -1
    /* 发送请求 */
    wx.request({
      url: 'http://112.74.105.17:20003/question-bank/wx/answerPVP/answerQuestion',
      method:"POST",
      dataType:"json",
      data: {
        "answers": [cont.id],
        "roomId": roomId,
        "stemId": cont.stemId,
      },
      success(res){
        if(res.data.code === 200){
          /* 更新答案情况 */
          thisPage.setData({answer: res.data.data, })
          let optionDtoList = thisPage.data.topic.optionDtoList
          for(let index = 0; index < optionDtoList.length; ++index){
            if(optionDtoList[index].content === thisPage.data.answer.rightAnswer[0]){
              rightIndex = index
              break
            }
          }
          if(rightIndex == -1){
            wx.showToast({
              title: '正在获取数据',
              icon: 'loading',
              duration: 1000
            })
            thisPage.answerTopic(option, reflushAnswers)
          } else reflushAnswers(option.target.dataset.index, rightIndex)
        }else{
          thisPage.answerTopic(option, reflushAnswers)
        }
      },
      fail(){
        wx.showToast({
          title: '正在获取数据',
          icon: 'loading',
          duration: 1000
        })
        thisPage.answerTopic(option, reflushAnswers)
      } 
    })
  },

})
